from network import httpconnection


def httpGetTest():
    url = "http://127.0.0.1/test"
    conn = httpconnection.HttpURLConnection(url)
    conn.setRequestProperty("CLIENT-HEADER-TEST", "OK")
    conn.setRequestData(bytearray("Hello Server".encode()))
    code = conn.getResponseCode()
    msg = conn.getResponseMessage()
    headers = conn.getHeaderFields()
    conn.close()
    print("httpGetTest>>>>>>>")
    print("url = " + url)
    print("response code = " + str(code) + ", message = " + msg)
    print("headers:")
    for kv in headers.items():
        print(kv[0] + ': ' + kv[1]);
    print("")

def httpPostTest():
    url = "http://127.0.0.1/test"
    conn = httpconnection.HttpURLConnection(url)
    conn.setRequestMethod("POST")
    conn.setRequestProperty("CLIENT-HEADER-TEST", "OK")
    conn.setRequestData(bytearray("echo".encode()))
    code = conn.getResponseCode()
    msg = conn.getResponseMessage()
    headers = conn.getHeaderFields()
    print("httpPostTest>>>>>>>")
    print("url = " + url)
    print("response code = " + str(code) + ", message = " + msg)
    print("headers:")
    for kv in headers.items():
        print(kv[0] + ': ' + kv[1]);
    print("body:")
    print(conn.getResponseData().decode())
    conn.close()


if __name__ == '__main__':
    httpGetTest()
    print("----------------------------")
    httpPostTest()
